package com.carkeeper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carkeeper.dto.CarDTO;
import com.carkeeper.exception.BussinessException;
import com.carkeeper.mapper.CarMapper;
import com.carkeeper.pojo.CUserPO;
import com.carkeeper.pojo.CarPO;
import com.carkeeper.service.CarService;
import com.carkeeper.vo.CarQueryParamVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class CarServiceImpl implements CarService {
    @Autowired
    private CarMapper carMapper;

    @Override
    public Boolean saveAndUpdate(CarPO carPO) {
        if (carPO.getId() != null) {
            carMapper.updateById(carPO);
        } else {
            carMapper.insert(carPO);
        }
        return true;
    }

    @Override
    public Boolean delete(Long id) {
        carMapper.deleteById(id);
        return true;
    }

    @Override
    public Page<CarPO> findByPage(CarQueryParamVo paramVo) {
        if (paramVo.getPage() == null) {
            paramVo.setPage(1);
        }
        if (paramVo.getPageSize() == null) {
            paramVo.setPageSize(10);
        }
        Page<CarPO> page = new Page<>(paramVo.getPage(), paramVo.getPageSize());
        CarPO paramPO = new CarPO();
        BeanUtils.copyProperties(paramVo, paramPO);
        LambdaQueryWrapper<CarPO> lambdaQueryWrapper = buildWrapper(paramPO, null);
        return carMapper.selectPage(page, lambdaQueryWrapper);
    }

    @Override
    public List<CarPO> list(CarDTO carDTO) {
        CarPO paramPO = new CarPO();
        BeanUtils.copyProperties(carDTO, paramPO);
        LambdaQueryWrapper<CarPO> lambdaQueryWrapper = buildWrapper(paramPO, carDTO.getIds());
        return carMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public CarPO findById(Long id) {
        return carMapper.selectById(id);
    }

    @Override
    public CarPO findByCode(String code) {
        if (StringUtils.isEmpty(code)) {
            throw new BussinessException("车牌号不能为空！");
        }
        LambdaQueryWrapper<CarPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CarPO::getCode, code);
        return carMapper.selectOne(lambdaQueryWrapper);
    }

    /**
     * 构建查询包装器
     *
     * @param carPO 查询条件
     * @param ids   查询条件
     * @return 查询包装器
     */
    private LambdaQueryWrapper<CarPO> buildWrapper(CarPO carPO, List<Long> ids) {
        LambdaQueryWrapper<CarPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ids != null && ids.size() > 0) {
            lambdaQueryWrapper.in(CarPO::getId, ids);
        }
        if (carPO.getBrandTypeId() != null) {
            lambdaQueryWrapper.eq(CarPO::getBrandTypeId, carPO.getBrandTypeId());
        }
        if (!StringUtils.isEmpty(carPO.getName())) {
            lambdaQueryWrapper.like(CarPO::getName, carPO.getName());
        }
        if (!StringUtils.isEmpty(carPO.getCode())) {
            lambdaQueryWrapper.like(CarPO::getCode, carPO.getCode());
        }
        if (!StringUtils.isEmpty(carPO.getSeries())) {
            lambdaQueryWrapper.like(CarPO::getSeries, carPO.getSeries());
        }
        if (!StringUtils.isEmpty(carPO.getMode())) {
            lambdaQueryWrapper.like(CarPO::getMode, carPO.getMode());
        }
        if (!StringUtils.isEmpty(carPO.getEngineCode())) {
            lambdaQueryWrapper.eq(CarPO::getEngineCode, carPO.getEngineCode());
        }
        if (!StringUtils.isEmpty(carPO.getFrameCode())) {
            lambdaQueryWrapper.eq(CarPO::getFrameCode, carPO.getFrameCode());
        }
        if (carPO.getUserId() != null) {
            lambdaQueryWrapper.eq(CarPO::getUserId, carPO.getUserId());
        }
        lambdaQueryWrapper.orderByDesc(CarPO::getCreateTime);
        return lambdaQueryWrapper;
    }
}
